home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / FGFADE11.ZIP / FADE.C < prev    next >
Text File  |  1995-02-13  |  8KB  |  219 lines

  1. /****************************************************************************\
  2. *                                                                            *
  3. *  FADE.C                                                                    *
  4. *                                                                            *
  5. *  This program demonstrates how to perform a smooth palette fade with       *
  6. *  Fastgraph.  This example assumes a 256-color video mode with 6-bit DAC    *
  7. *  values (i.e., between 0 and 63).  These values are defined at the top of  *
  8. *  this file, so you can change them easily.                                 *
  9. *                                                                            *
  10. *  The fadein() and fadeout() routines in this program were originally       *
  11. *  written by John Wagner, author of the IMPROCES image processing program.  *
  12. *                                                                            *
  13. *  To compile this program and link it with Fastgraph version 4.0:           *
  14. *                                                                            *
  15. *     Borland C++:                                                           *
  16. *        BCC -ms FADE.C FGS.LIB                                              *
  17. *                                                                            *
  18. *     Microsoft C/C++ and Visual C++:                                        *
  19. *        CL /AS FADE.C /link FGS                                             *
  20. *                                                                            *
  21. *     Microsoft QuickC:                                                      *
  22. *        QCL /AS FADE.C /link FGS                                            *
  23. *                                                                            *
  24. *     Microsoft Visual C++ 32-bit Edition with Phar Lap TNT extender:        *
  25. *        CL FADE.C /link /stub:\TNT\BIN\GOTNT.EXE FG32VC.LIB                 *
  26. *                                                                            *
  27. *     Power C:                                                               *
  28. *        PC /ms FADE                                                         *
  29. *        PCL FADE ;FGS                                                       *
  30. *                                                                            *
  31. *     Turbo C and Turbo C++:                                                 *
  32. *        TCC -ms FADE.C FGS.LIB                                              *
  33. *                                                                            *
  34. *     Watcom C/C++ (16 bits):                                                *
  35. *        WCL /ms FADE.C FGS.LIB                                              *
  36. *                                                                            *
  37. *     Watcom C/C++ (32 bits) with Rational Systems DOS/4GW extender:         *
  38. *        WCL386 /l=dos4g FADE.C FG32.LIB FG32DPMI.LIB                        *
  39. *                                                                            *
  40. *  This program also can be linked with Fastgraph/Light 4.0 if you replace   *
  41. *  the FGS library references with FGLS.                                     *
  42. *                                                                            *
  43. *  Fastgraph (tm) and Fastgraph/Light (tm) are graphics libraries published  *
  44. *  by Ted Gruber Software.  For more info, please call, write, or FAX.       *
  45. *                                                                            *
  46. *  Ted Gruber Software                           orders/info (702) 735-1980  *
  47. *  PO Box 13408                                          FAX (702) 735-4603  *
  48. *  Las Vegas, NV  89112                                  BBS (702) 796-7134  *
  49. *                                                                            *
  50. \****************************************************************************/
  51.  
  52. #include <fastgraf.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56.  
  57. /* function prototypes */
  58.  
  59. void main(void);
  60. void fadein(char *,int);
  61. void fadeout(int);
  62.  
  63. /* these values can be changed for different video modes */
  64.  
  65. #define NDACS 256
  66. #define NCOLORS 64
  67. #define VIDEO_MODE 19
  68.  
  69. /* this typedef struct is a clean way to do DACs */
  70.  
  71. typedef struct
  72. {
  73.    unsigned char r, g, b;
  74. }
  75. RGB;
  76.  
  77. /* these global arrays hold two complete sets of DAC values */
  78.  
  79. RGB dacs1[NDACS], dacs2[NDACS];
  80.  
  81. /* main program */
  82.  
  83. void main()
  84. {
  85.    int delay;
  86.    int old_mode;
  87.  
  88.    /* in case we're compiling for protected mode */
  89.  
  90.    fg_initpm();
  91.  
  92.    /* make sure the requested graphics mode is available */
  93.  
  94.    if (fg_testmode(VIDEO_MODE,1) == 0)
  95.    {
  96.       printf("This program requires a %d-color graphics mode.\n",NDACS);
  97.       exit(1);
  98.    }
  99.  
  100.    /* calculate the base delay between DAC updates */
  101.  
  102.    delay = fg_measure() / 128;
  103.  
  104.    /* initialize Fastgraph for the requested video mode */
  105.  
  106.    old_mode = fg_getmode();
  107.    fg_setmode(VIDEO_MODE);
  108.  
  109.    /* for each PCX file, fade it in and then back out */
  110.  
  111.    fadein("TOMMY.PCX",delay);
  112.    fg_waitfor(36);
  113.    fadeout(delay);
  114.    fg_waitfor(18);
  115.  
  116.    fadein("BALLOONS.PCX",delay*2);
  117.    fg_waitfor(36);
  118.    fadeout(delay*2);
  119.    fg_waitfor(18);
  120.  
  121.    fadein("MOUSE.PCX",delay*4);
  122.    fg_waitfor(36);
  123.    fadeout(delay*4);
  124.  
  125.    /* restore the original video mode and screen attributes */
  126.  
  127.    fg_setmode(old_mode);
  128.    fg_reset();
  129. }
  130.  
  131. /****************************************************************************\
  132. *                                                                            *
  133. *  fadein                                                                    *
  134. *                                                                            *
  135. *  Display an image by gradually increasing each DAC's RGB components to     *
  136. *  their original values.                                                    *
  137. *                                                                            *
  138. \****************************************************************************/
  139.  
  140. void fadein(PCXfile,delay)
  141. char *PCXfile;
  142. int delay;
  143. {
  144.    register int i, j;
  145.    int target;
  146.  
  147.    /* get the target DAC values from the PCX file */
  148.  
  149.    fg_pcxpal(PCXfile,(unsigned char *)dacs1);
  150.  
  151.    /* zero all of the DACs */
  152.  
  153.    memset(dacs2,NDACS*3,0);
  154.    fg_setdacs(0,NDACS,(unsigned char *)dacs2);
  155.  
  156.    /* display the blacked-out PCX image */
  157.  
  158.    fg_showpcx(PCXfile,1);
  159.  
  160.    /* cycle through the DACs, gradually increasing them to their old values */
  161.  
  162.    for (j = 0; j < NCOLORS; j++)
  163.    {
  164.       /* increment each RGB component if it is below its old value */
  165.  
  166.       target = NCOLORS - j;
  167.  
  168.       for (i = 0; i < NDACS; i++)
  169.       {
  170.          if (dacs1[i].r > target && dacs2[i].r < dacs1[i].r) dacs2[i].r++;
  171.          if (dacs1[i].g > target && dacs2[i].g < dacs1[i].g) dacs2[i].g++;
  172.          if (dacs1[i].b > target && dacs2[i].b < dacs1[i].b) dacs2[i].b++;
  173.       }
  174.  
  175.       /* update the DACs each time through the loop */
  176.  
  177.       fg_stall(delay);
  178.       fg_setdacs(0,NDACS,(unsigned char *)dacs2);
  179.    }
  180. }
  181.  
  182. /****************************************************************************\
  183. *                                                                            *
  184. *  fadeout                                                                   *
  185. *                                                                            *
  186. *  Erase an image by gradually fading each DAC's RGB components to black.    *
  187. *                                                                            *
  188. \****************************************************************************/
  189.  
  190. void fadeout(delay)
  191. int delay;
  192. {
  193.    register int i, j;
  194.  
  195.    /* load the dacs1 and dacs2 arrays with the current DAC values */
  196.  
  197.    fg_getdacs(0,NDACS,(unsigned char *)dacs1);
  198.    memcpy(dacs2,dacs1,NDACS*3);
  199.  
  200.    /* cycle through the DACs, gradually reducing them to 0 (black) */
  201.  
  202.    for (j = 0; j < NCOLORS; j++)
  203.    {
  204.       /* decrement each RGB component if it is above 0 */
  205.  
  206.       for (i = 0; i < NDACS; i++)
  207.       {
  208.          if (dacs2[i].r) dacs2[i].r--;
  209.          if (dacs2[i].g) dacs2[i].g--;
  210.          if (dacs2[i].b) dacs2[i].b--;
  211.       }
  212.  
  213.       /* update the DACs each time through the loop */
  214.  
  215.       fg_stall(delay);
  216.       fg_setdacs(0,NDACS,(unsigned char *)dacs2);
  217.    }
  218. }
  219.